﻿/******************************************************************************
	Cobalt - utility libraries for building XNA games
	Copyright (C) 2008 Philip Djonov

	This program is free software; you can redistribute it and/or modify it
	under the terms of the GNU General Public License as published by the Free
	Software Foundation; either version 2 of the License, or (at your option)
	any later version.

	This program is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
	for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the
	
		Free Software Foundation, Inc.
		51 Franklin Street, Fifth Floor
		Boston, MA  02110-1301, USA.
******************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Graphics;

namespace Cobalt.Content.Pipeline.Quake.Shaders
{

	public static class ShaderContentHelpers
	{

		public static IEnumerable<string> GetReferencedTextures( this Shader shader )
		{
			Dictionary<string, bool> ret = new Dictionary<string, bool>();

			foreach( var stage in shader.Stages )
			{
				foreach( var bundle in stage.Bundles )
				{
					foreach( string tex in bundle.Images )
						ret[tex] = true;
				}
			}

			return ret.Keys;
		}

		public static IEnumerable<TextureBundle> GetBundles( this Shader shader )
		{
			foreach( var stage in shader.Stages )
			{
				foreach( var bundle in stage.Bundles )
					yield return bundle;
			}
		}

		public static bool ReferencesTexture( this Shader shader, string texture )
		{
			return shader.GetReferencedTextures().Contains( texture );
		}

		public static bool IsConstantBundle( this TextureBundle bundle, out Vector4 value )
		{
			if( bundle.Images.Count == 1 )
			{
				if( bundle.Images[0] == TextureBundle.WhiteImage )
				{
					value = Vector4.One;
					return true;
				}
			}

			value = new Vector4();
			return false;
		}

		public static bool IsConstantBundle( this TextureBundle bundle )
		{
			Vector4 value;
			return IsConstantBundle( bundle, out value );
		}

		public static bool IsPomBundle( this TextureBundle bundle )
		{
			return bundle.TexCoordGen is TextureTexCoordGen &&
				bundle.TexCoordMods.Count == 1 &&
				bundle.TexCoordMods[0] is PomTexCoordMod;
		}
	}

}